extends IBasicComponent


class_name ItemContainer
"""
用于管理多个组件的集合
"""


## 适配append和remove
@export var item_appender: ItemAppender = null
## 挂载的组件
@export var items: Array[Node] = []


## 挂载组件时通知
signal event_append_component(cmpt: ItemContainer, node: Node)
## 移除组件时的通知
signal event_remove_component(cmpt: ItemContainer, node: Node)
## 组件更新时通知
signal event_update_component(cmpt: ItemContainer, node: Node)


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "item_appender": [IComponent.find_child.bind(self, func(x): return x is ItemAppender, item_appender)]
    }, super.__check_exports())

func append(node: Node) -> void:
    if item_appender:
        item_appender.append(node)
    else:
        items.push_back(node)
    event_append_component.emit(self, node)

func update(node: Node) -> void:
    event_update_component.emit(self, node)

func remove(node: Node) -> void:
    if item_appender:
        item_appender.remove(node)
    else:
        items.erase(node)
    event_remove_component.emit(self, node)


func reset():
    items.clear()
    super.__reset()

func size() -> int:
    return items.size()

func is_empty() -> bool:
    return size() < 1

func get_at(index: int) -> Node:
    return items[index]

func has(node: Node) -> bool:
    return items.has(node)
